package com.ldzy.cart;

import com.ldzy.common.pojo.Goods;
import com.ldzy.common.pojo.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MyController {
    @Autowired
    private GoodsApi goodsApi;
    @Autowired
    private OrderApi orderApi;

    @GetMapping("/cart/hello")
    public String demo(HttpServletRequest request){
        //读取到达这个方法的请求头部
        String id = request.getHeader("user-info");
        //假如用户123456789的购物车商品id:3,5,分别数量为3，5，去商品微服务中查询商品名称，和价格信息
        Goods goods3 = goodsApi.getGoodsById(3L);
        Goods goods5 = goodsApi.getGoodsById(5L);
        return "goods3:"+goods3.toString()+"-------"+"goods5:"+goods5.toString();
    }

    @GetMapping("/cart/order")//在购物车中提交订单
    @GlobalTransactional
    public String commitOrder(){
        Goods goods=new Goods();
        goods.setId(1L);
        goods.setCount(5);//需要购买5件商品

        Order order = new Order();
        order.setGid(goods.getId());
        order.setCount(goods.getCount());

        //1、调用订单微服务新建订单
        boolean is=orderApi.newOrder(order);
        System.out.println(is);
        //2、删除购物车的对应项目
        System.out.println("删除购物车的对应项目");
        //3、调用商品微服务减少商品库存
        boolean isUpdate=goodsApi.update(goods);
        System.out.println(isUpdate);
        return "下单成功！";
    }
    @GetMapping("/cart/query")
    public String query(){
        return "查询成功";
    }
}
